package com.lkm.app_ui.help;

import java.io.ObjectStreamException;
import java.io.Serializable;
import android.content.Context;
import android.graphics.PixelFormat;
import android.view.Gravity;
import android.view.View;
import android.view.ViewManager;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;


public class WindowViewHelp implements Serializable {


	private static final long serialVersionUID = 1L;
	private WindowViewHelp(){}
	private static WindowViewHelp mWindowViewHelp;
	private static int instancec=0;
	public static WindowViewHelp instance(){
		if(instancec==0)
			synchronized (WindowViewHelp.class) {
				if(instancec==0){
					mWindowViewHelp=new WindowViewHelp();
					instancec=1;
				}
			}
		return mWindowViewHelp;
	}


	private Object readResolve() throws ObjectStreamException {


		return instance();
	}
	public void closeView(View view){

		Object o= view.getParent();
		if(o instanceof ViewManager)
			((ViewManager)o).removeView(view);

	}
	WindowManager wm;
	public void showView(View view,Context mContext,int x,int y){

		WindowManager.LayoutParams params = (WindowManager.LayoutParams)view.getLayoutParams();
		closeView(view);
		params.type = WindowManager.LayoutParams.TYPE_PHONE;//TYPE_SYSTEM_ALERT ;//| WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY;
//		params.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
		params.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
//		params.width = WindowManager.LayoutParams.WRAP_CONTENT;
//		params.height = WindowManager.LayoutParams.WRAP_CONTENT;
		params.format=PixelFormat.RGBA_8888;//android.graphics.PixelFormat.TRANSLUCENT;

		params.gravity=Gravity.LEFT|Gravity.TOP;

		params.x = x;
		params.y= y;
		getWindowManager(mContext).addView(view, params);
		view.setVisibility(View.VISIBLE);

	}
	public void showView(View view,Context mContext){

		WindowManager.LayoutParams params = (WindowManager.LayoutParams)view.getLayoutParams();
		closeView(view);
		params.type = WindowManager.LayoutParams.TYPE_PHONE;//TYPE_SYSTEM_ALERT ;//| WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY;
		params.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
		params.format=PixelFormat.RGBA_8888;//android.graphics.PixelFormat.TRANSLUCENT;
		getWindowManager(mContext).addView(view, params);
		view.setVisibility(View.VISIBLE);

	}
	public void showView_notFocus(View view,Context mContext){

		WindowManager.LayoutParams params = (WindowManager.LayoutParams)view.getLayoutParams();
		closeView(view);
		params.type = WindowManager.LayoutParams.TYPE_PHONE;//TYPE_SYSTEM_ALERT ;//| WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY;
		params.flags =WindowManager.LayoutParams.FLAG_FULLSCREEN| WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE|WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL|WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
		params.format=PixelFormat.RGBA_8888;//android.graphics.PixelFormat.TRANSLUCENT;
		getWindowManager(mContext).addView(view, params);
		view.setVisibility(View.VISIBLE);

	}

	public WindowManager getWindowManager(Context mContext){
		if(wm==null){
			wm= (WindowManager)mContext.getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
		}
		return wm;
	}


	public void updatePosition(View v,LayoutParams l){
		if(wm!=null)
			wm.updateViewLayout(v,l);
	}

}
